package com.quanxi.qxaimain.controller.v1.dataHelper;

import com.quanxi.qxaimain.common.message.ymlConfig;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanxi.qxaimain.common.util.RestTemplateUtils.RestTemplateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author Liang Zhang
 * @date 2022/09/05
 */
@Slf4j
@RestController
@RequestMapping("/dataProcess")
@Api(tags = "数据处理")
public class RegainDataController {
    @Resource
    private ymlConfig ymlCofig;

    @PostMapping("/dataHelper")
    @ApiOperation(value = "补抽数据", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "params" ,value = "参数格式: json;  参数说明：discharge_date_ym: 出院时间年与月 e.g. \"2022-03\"  ind_id: 指标编号名称  e.g. \"c8\"", required = true, dataType = "json", paramType = "body")})
    public JSONObject chapterThird(@RequestBody JSONObject params) throws ParseException {
        JSONObject data = params.getJSONArray("data").getJSONObject(0);
        String str1 = data.getString( "discharge_date_ym");
        String str2 = data.getString( "ind_id");


        String url = ymlCofig.getRegainDataUrl()+"/api/v1/qxqcsdbapi/indicators/updateIndicatorResults";
        JSONArray requestBodyArr = new JSONArray();
        JSONObject requestBody = new JSONObject();
        List<String> list =new ArrayList<>();
        requestBody.put("month", str1);
        requestBody.put("kpiCode", str2);
        requestBodyArr.add(requestBody);
        System.out.println(requestBodyArr);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, requestBodyArr, JSONObject.class);

        Map<String, Object> result = new HashMap<>();
        result.put("discharge_date_ym", str1);
        result.put("ind_id", str2);
        JSONObject jsonObj = new JSONObject(result);
        return response.getBody();
    }
}
